package com.pdg.android.stockmarket;

import android.widget.Button;
import android.widget.TextView;

import com.pdg.android.common.State;

public class DashboardState extends State<GameData,StockMarketActivity,GameState>{
	
	private static final int[] STOCK_NAME_IDS = {
		R.id.row_stock_name_1,
		R.id.row_stock_name_2,
		R.id.row_stock_name_3,
		R.id.row_stock_name_4,
		R.id.row_stock_name_5,
		R.id.row_stock_name_6,
		R.id.row_stock_name_7,
		R.id.row_stock_name_8,
		R.id.row_stock_name_9,
		R.id.row_stock_name_10
		};
	private static final int[] STOCK_SHARES_IDS = {
		R.id.row_stock_shares_1,
		R.id.row_stock_shares_2,
		R.id.row_stock_shares_3,
		R.id.row_stock_shares_4,
		R.id.row_stock_shares_5,
		R.id.row_stock_shares_6,
		R.id.row_stock_shares_7,
		R.id.row_stock_shares_8,
		R.id.row_stock_shares_9,
		R.id.row_stock_shares_10,
	};
	private static final int[] STOCK_BUY_IDS = {
		R.id.row_button_buy_1,
		R.id.row_button_buy_2,
		R.id.row_button_buy_3,
		R.id.row_button_buy_4,
		R.id.row_button_buy_5,
		R.id.row_button_buy_6,
		R.id.row_button_buy_7,
		R.id.row_button_buy_8,
		R.id.row_button_buy_9,
		R.id.row_button_buy_10,
	};
	private static final int[] STOCK_SELL_IDS = {
		R.id.row_button_sell_1,
		R.id.row_button_sell_2,
		R.id.row_button_sell_3,
		R.id.row_button_sell_4,
		R.id.row_button_sell_5,
		R.id.row_button_sell_6,
		R.id.row_button_sell_7,
		R.id.row_button_sell_8,
		R.id.row_button_sell_9,
		R.id.row_button_sell_10,
	};

	public DashboardState(GameData theData, StockMarketActivity theMachine) {
		super(theData, theMachine, GameState.DASHBOARD);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onInitialize() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onEnter() {
		getMachine().setContentView(R.layout.dashboard);
		refreshDisplay();
	}

	private void refreshDisplay() {
		for(int theIndex=GameData.STOCK_FIRST;theIndex<GameData.STOCK_COUNT;++theIndex){
			Stock theStock = getData().getStocks()[theIndex];
			TextView tv = (TextView)getMachine().findViewById(STOCK_NAME_IDS[theIndex]);
			tv.setText(String.format(
					getMachine().getString(R.string.stock_name_format),
					theStock.getName(),
					theStock.getValue()
					));
			tv = (TextView)getMachine().findViewById(STOCK_SHARES_IDS[theIndex]);
			tv.setText(String.format(
					getMachine().getString(R.string.stock_shares_format),
					theStock.getShares()
					));
			tv = (TextView)getMachine().findViewById(R.id.cash_available);
			tv.setText(String.format(
					getMachine().getString(R.string.cash_available_format),
					getData().getPlayerMoney()
					));
			Button button = (Button)getMachine().findViewById(STOCK_BUY_IDS[theIndex]);
			button.setEnabled(getData().getPlayerMoney()>=theStock.getValue());
			button = (Button)getMachine().findViewById(STOCK_SELL_IDS[theIndex]);
			button.setEnabled(theStock.getShares()>0);
		}
	}

	@Override
	protected void onLeave() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onCleanUp() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onButtonPressed(int theButtonId) {
		switch(theButtonId){
		case R.id.button_main_menu:
			getMachine().setCurrent(GameState.MAIN_MENU);
			break;
		case R.id.button_report:
			getMachine().setCurrent(GameState.REPORT);
			break;
		case R.id.button_end_turn:
			getData().endTurn(getMachine());
			if(getData().isGameInProgress()){
				getMachine().setCurrent(GameState.REPORT);
			}else{
				getMachine().setCurrent(GameState.END_GAME);
			}
			break;
		case R.id.row_button_buy_1:
			getData().setCurrentStock(0);
			getMachine().setCurrent(GameState.BUY);
			break;
		case R.id.row_button_buy_2:
			getData().setCurrentStock(1);
			getMachine().setCurrent(GameState.BUY);
			break;
		case R.id.row_button_buy_3:
			getData().setCurrentStock(2);
			getMachine().setCurrent(GameState.BUY);
			break;
		case R.id.row_button_buy_4:
			getData().setCurrentStock(3);
			getMachine().setCurrent(GameState.BUY);
			break;
		case R.id.row_button_buy_5:
			getData().setCurrentStock(4);
			getMachine().setCurrent(GameState.BUY);
			break;
		case R.id.row_button_buy_6:
			getData().setCurrentStock(5);
			getMachine().setCurrent(GameState.BUY);
			break;
		case R.id.row_button_buy_7:
			getData().setCurrentStock(6);
			getMachine().setCurrent(GameState.BUY);
			break;
		case R.id.row_button_buy_8:
			getData().setCurrentStock(7);
			getMachine().setCurrent(GameState.BUY);
			break;
		case R.id.row_button_buy_9:
			getData().setCurrentStock(8);
			getMachine().setCurrent(GameState.BUY);
			break;
		case R.id.row_button_buy_10:
			getData().setCurrentStock(9);
			getMachine().setCurrent(GameState.BUY);
			break;
		case R.id.row_button_sell_1:
			getData().setCurrentStock(0);
			getMachine().setCurrent(GameState.SELL);
			break;
		case R.id.row_button_sell_2:
			getData().setCurrentStock(1);
			getMachine().setCurrent(GameState.SELL);
			break;
		case R.id.row_button_sell_3:
			getData().setCurrentStock(2);
			getMachine().setCurrent(GameState.SELL);
			break;
		case R.id.row_button_sell_4:
			getData().setCurrentStock(3);
			getMachine().setCurrent(GameState.SELL);
			break;
		case R.id.row_button_sell_5:
			getData().setCurrentStock(4);
			getMachine().setCurrent(GameState.SELL);
			break;
		case R.id.row_button_sell_6:
			getData().setCurrentStock(5);
			getMachine().setCurrent(GameState.SELL);
			break;
		case R.id.row_button_sell_7:
			getData().setCurrentStock(6);
			getMachine().setCurrent(GameState.SELL);
			break;
		case R.id.row_button_sell_8:
			getData().setCurrentStock(7);
			getMachine().setCurrent(GameState.SELL);
			break;
		case R.id.row_button_sell_9:
			getData().setCurrentStock(8);
			getMachine().setCurrent(GameState.SELL);
			break;
		case R.id.row_button_sell_10:
			getData().setCurrentStock(9);
			getMachine().setCurrent(GameState.SELL);
			break;
		}
	}

}
